﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Light.Utility
{
    /// <summary>
    /// DataTable列操作项
    /// </summary>
    public class OperateColumn
    {
        private List<string> includes = null;
        private List<string> excludes = null;
        private Dictionary<string, string> dicNewNames = null;

        public OperateColumn()
        {
            includes = new List<string>();
            excludes = new List<string>();
            dicNewNames = new Dictionary<string, string>();
        }

        /// <summary>
        /// 设置需要保留的列，如果设置了需要排除的列，则优先保留未排除的项，无需设置Retain
        /// </summary>
        public void Retain(string columName)
        {
            if (!includes.Contains(columName))
                includes.Add(columName);
            if (HasNewNames)
            {
                foreach (var oldName in dicNewNames.Keys)
                {
                    if (!includes.Contains(oldName))
                        includes.Add(oldName);
                }
            }
        }

        /// <summary>
        /// 重命名列
        /// </summary>
        /// <param name="columName">原列名</param>
        /// <param name="newName">新列名</param>
        public void Rename(string columName, string newName)
        {
            if (HasIncludes && !includes.Contains(columName))
                includes.Add(columName);

            if (dicNewNames.ContainsKey(columName))
                dicNewNames[columName] = newName;
            else
                dicNewNames.Add(columName, newName);
        }

        /// <summary>
        /// 需要排除的列
        /// </summary>
        public void Remove(string columnName)
        {
            if (!excludes.Contains(columnName))
                excludes.Add(columnName);
        }

        /// <summary>
        ///  是否包含要查询的列
        /// </summary>
        public bool HasIncludes
        {
            get
            {
                return includes.Count > 0;
            }
        }

        /// <summary>
        ///  是否包含要排除的列
        /// </summary>
        public bool HasExcludes
        {
            get
            {
                return excludes.Count > 0;
            }
        }

        /// <summary>
        /// 是否保留列
        /// </summary>
        /// <param name="columnName"></param>
        /// <returns></returns>
        public bool IsRetain(string columnName)
        {
            bool contains = Include(columnName);
            if (!contains && HasExcludes)
            {
                contains = !Exclude(columnName);
            }
            return contains;
        }

        /// <summary>
        /// 是否有重命名列
        /// </summary>
        public bool HasNewNames
        {
            get
            {
                return dicNewNames.Count > 0;
            }
        }

        /// <summary>
        /// 获取新的列名
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, string> GetNewNames()
        {
            return dicNewNames;
        }

        private bool Include(string columnName)
        {
            return (HasIncludes && includes.Contains(columnName)) || !HasIncludes;
        }

        private bool Exclude(string columnName)
        {
            return HasExcludes && excludes.Contains(columnName);
        }
    }
}
